<?php

namespace app\api\controller\product;

use think\Controller;

class Product extends Controller{

    public function __construct()
    {
        parent::__construct();

        $this->ProductModel = model('Product.Product');
        $this->TypeModel = model('Product.Type');
        $this->VipModel = model('Product.Vip');
        $this->UserModel = model('User.User');
    }

    // 首页
    public function index()
    {
        if($this->request->isPost())
        {
            // 分类列表
            $typeList = $this->TypeModel->select();

            // 推荐
            $recommendList = $this->ProductModel->where(['flag' => 3])->order('id', 'DESC')->limit(3)->select();

            // 新品
            $newList = $this->ProductModel->where(['flag' => 1])->order('id', 'DESC')->limit(4)->select();

            // 热销
            $HotList = $this->ProductModel->where(['flag' => 2])->order('id', 'DESC')->limit(6)->select();

            $data = [
                'typeList' => $typeList,
                'recommendList' => $recommendList,
                'newList' => $newList,
                'HotList' => $HotList,
            ];

            //跳转界面
            $this->success('成功', null, $data);
            exit;
        }
    }

    // 产品中心
    public function type()
    {
        if($this->request->isPost())
        {
            $typeid = $this->request->param('typeid', 0);
            $keywords = $this->request->param('keywords', '');
            $flag = $this->request->param('flag', 0);
            $orderby = $this->request->param('orderby', 'id');

            $where = [];

            if($typeid)
            {
                $where['typeid'] = $typeid;
            }

            if(!empty($keywords)){
                $where['name'] = ['like',"%$keywords%"];
            }

            if($flag > 0){
                $where['flag'] = $flag;
            }

            // 产品列表
            $productList = $this->ProductModel->where($where)->order($orderby,'DESC')->select();

            // 分类列表
            $typeList = $this->TypeModel->select();

            $data = [
                'typeList' => $typeList,
                'productList' => $productList,
            ];

            //跳转界面
            $this->success('成功', null, $data);
            exit;
        }
    }

    // 产品详情
    public function info()
    {
        if($this->request->isPost())
        {
            $proid = $this->request->param('proid',0);
            $userid = $this->request->param('userid', 0);

            $userinfo = $this->UserModel->find($userid);
            
            //当没有找到用户的情况下
            if(!$userinfo){
                $this->error('用户不存在，请重新输入');
                exit;
            }

            $productInfo = $this->ProductModel->find($proid);

            //产品详情
            if(!$productInfo){

                $this->error($this->ProductModel->getError());
                exit;
            } else{

                $vip = isset($userinfo) ? $userinfo['vip'] : 0;

                // 查询产品vip的价格
                if($vip > 0 ){
                    $level = $this->VipModel->where(['proid'=> $proid, 'level' => $vip])->value('price');
                    $productInfo['level'] = $level;
                }

                $this->success('成功', null, $productInfo);
                exit;
            }
        }
    }
}
?>